import global from '@/views/Global.vue'
let videos = {
    methods: {
        // 添加图标
        videoAddIconMarker(position, clickFunc) {
            let videoEntity = global.viewer.entities.add({
                id: 'videoModel',
                position: Cesium.Cartesian3.fromDegrees(position[0], position[1], 150),
                billboard: {
                    // 图像地址，URI或Canvas的属性
                    image: require('../assets/images/position2.png'),
                    scale: 0.05,
                },
                label: {
                    text: '点击查看施工动画模拟',
                    font: '30px Helvetica', // 15pt monospace
                    scale: 0.5,
                    style: Cesium.LabelStyle.FILL,
                    fillColor: Cesium.Color.WHITE,
                    outlineColor: Cesium.Color.BLACK,
                    outlineWidth: 1,
                    pixelOffset: new Cesium.Cartesian2(0, -40), //偏移量
                }
            });
            global.entArr.push(videoEntity)
            videoEntity.heightReference = Cesium.HeightReference.CLAMP_TO_GROUND;
            if (clickFunc) {
                videoEntity['videoClick'] = clickFunc;
            }
        },
        // 移除所有图标
        videoRemove() {
            if (entArr.length > 0) {
                global.entArr.forEach((item) => {
                    global.viewer.entities.remove(item)
                })
            }
            global.entArr = []; // 清空数组
        },
        // 点击图标添加信息窗
        videoDialog(videoId, videoPosition, videoType) {
            const videoBox = document.getElementById(videoId);
            videoBox.style.display = 'block';
            global.dialogBox.push({
                video: videoBox,
                videoPosition: videoPosition,
                videoType: videoType
            })

            if (global.dialogBoxPreRenderListener) {
                return;
            } else {
                global.dialogBoxPreRenderListener = global.viewer.scene.preRender.addEventListener(function() {
                    global.dialogBox.forEach(item => {
                        let elWidth = item.video.offsetWidth - 700;
                        let elHeight = item.video.offsetHeight - 70;
                        const scratch = new Cesium.Cartesian2(); // cesium二维笛卡尔 笛卡尔二维坐标系就是我们熟知的而二维坐标系；三维也如此
                        let position = null;
                        if (item.videoType == 'Cartesian3') {
                            position = item.videoPosition;
                        } else if (item.videoType == 'LonLat') {
                            position = Cesium.Cartesian3.fromDegrees(item.videoPosition[0], item.videoPosition[1], 20);
                        }
                        const canvasPosition = global.viewer.scene.cartesianToCanvasCoordinates(position, scratch);
                        if (Cesium.defined(canvasPosition)) {
                            // item.video.style.left = canvasPosition.x - elWidth / 2 + 'px';
                            // item.video.style.top = canvasPosition.y - elHeight + 'px'
                            item.video.style.left = 0;
                            item.video.style.top = 0
                        }
                    })
                })
            }
        },
        // 移除绑定到图标上的弹窗
        removeVideoDialog() {
            if (global.dialogBox && global.dialogBox.length && global.dialogBox.length > 0) {
                global.dialogBox.forEach(item => {
                    item.video.style.display = 'none';
                });
                global.dialogBox = [];
            }
            if (global.dialogBoxPreRenderListener) {
                global.dialogBoxPreRenderListener()
                global.dialogBoxPreRenderListener = null
            }
        },
        // 监听
        setVideoHandler() {
            global.videoHandler = new Cesium.ScreenSpaceEventHandler(global.viewer.scene.canvas);
            global.videoHandler.setInputAction(function(movement) {
                let pickedObject = global.viewer.scene.pick(movement.position);
                if (Cesium.defined(pickedObject) && Cesium.defined(pickedObject.id) && typeof pickedObject.id.videoClick === 'function') {
                    pickedObject.id.videoClick(pickedObject.id, movement);
                } else if (Cesium.defined(pickedObject) && Cesium.defined(pickedObject.primitive && typeof pickedObject.primitive.videoClick === 'function')) {
                    pickedObject.primitive.videoClick(pickedObject, movement);
                }
            }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
        },
        // 移除监听
        removeHandler() {
            if (handler) {
                global.videoHandler.destroy();
                global.videoHandler = null
            }
        }
    },
}
export default videos